﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace Estimate.Common.Estimate
{
    public class WaterScore
    {
         [JsonIgnore]
        public int NA = 0;
         [JsonIgnore]
        public int NB = 0;
         [JsonIgnore]
        public int NC = 0;
         [JsonIgnore]
        public int ND = 0;
        /// <summary>
        /// 故障
        /// </summary>
        public int Breakdown = 0;
        /// <summary>
        /// 空预报
        /// </summary>
        public int EmptyForecast = 0;

        /// <summary>
        /// 预报少于stationmatch里的站点
        /// </summary>
        public int LackForecast = 0;

        /// <summary>
        /// 预报错误数据
        /// </summary>
        public int ForecastError = 0;

        /// <summary>
        /// 实况错误数据
        /// </summary>
        public int RealError = 0;
        /// <summary>
        /// 没有实况数据。实况漏
        /// 实况少于stationmatch里的站点
        /// </summary>
        public int LackReal = 0;

       /// <summary>
       /// 无意义的值
       /// </summary>
        [JsonIgnore]
        public readonly float NO_MEANS = 101f;

        public WaterScore() { }



        /// <summary>
        /// 计算总晴雨评定
        /// </summary>
        /// <returns></returns>
        public float CalPC()
        {
            float result;

            if (NA +NB +NC+ ND== 0)
            {
                result = NO_MEANS;
            }
            else
            {
                result = (NA + ND) * 100.0f / (NA +NB +NC+ ND);
            }
            return result;
        }

        /// <summary>
        /// 计算总晴雨评定
        /// </summary>
        public float PC
        {
            get
            {
                float result = CalPC();
                if (result == NO_MEANS)
                    return 0f;
                return result;
            }
        }

        /// <summary>
        ///TS评分算法
       /// </summary>
       /// <returns>TS结果</returns>
        public float CalTS()
        {
            float result = 0;

            if (NA + NB + NC == 0)
            {
                result = NO_MEANS;
            }
            else
            {
                result = NA * 100f / (NA + NB + NC);
            }

            return result;
        }

        /// <summary>
        ///TS评分算法
        /// </summary>
        public float TS
        {
            get
            {
                float result = CalTS();
                if (result == NO_MEANS)
                    return 0f;
                return result;
            }
        }

        /// <summary>
        /// 漏报率
        /// </summary>
        /// <returns></returns>       
        public float CalPO()
        {
            float result = 0;

            if (NA + NC == 0)
            {
                result = NO_MEANS;
            }
            else
            {
                result = NC * 100f / (NA + NC);
            }

            return result;
        }

        /// <summary>
        /// 漏报率
        /// </summary>
        public float PO
        {
            get
            {
                float result = CalPO();
                if (result == NO_MEANS)
                    return 0f;
                return result;
            }
        }

        /// <summary>
        ///空报率
        /// </summary>
        /// <returns>如果无意义,则值为 NO_MEANS,否则表示空报率</returns>       
        public float CalFAR()
        {
            float result = 0;

            if (NA + NB == 0)
            {
                result = NO_MEANS;
            }
            else
            {
                result = NB * 100f / (NA + NB);
            }

            return result;
        }

        /// <summary>
        ///空报率
        /// </summary>
        public float FAR
        {
            get
            {
                float result = CalFAR();
                if (result == NO_MEANS)
                    return 0f;
                return result;
            }
        }

        public override string ToString()
        {
            return string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}",new object[]{
                        NA,
                        NB,
                        NC,
                        ND,
                        EmptyForecast,
                        LackForecast,
                        ForecastError,
                        RealError,
                        LackReal,
                        Breakdown
                    });
        }

        public void FromString(string value)
        {
            string[] valueArray = Regex.Split(value, @"\,");
            NA = Convert.ToInt32(valueArray[0]);
            NB = Convert.ToInt32(valueArray[1]);
            NC = Convert.ToInt32(valueArray[2]);
            ND = Convert.ToInt32(valueArray[3]);
            EmptyForecast = Convert.ToInt32(valueArray[4]);
            LackForecast = Convert.ToInt32(valueArray[5]);
            ForecastError = Convert.ToInt32(valueArray[6]);
            RealError = Convert.ToInt32(valueArray[7]);
            LackReal = Convert.ToInt32(valueArray[8]);
            Breakdown = Convert.ToInt32(valueArray[9]);
        }

        public void Add(WaterScore score)
        {
            this.NA += score.NA;
            this.NB += score.NB;
            this.NC += score.NC;
            this.ND += score.ND;

            this.Breakdown += score.Breakdown;
            this.EmptyForecast += score.EmptyForecast;
            this.LackForecast += score.LackForecast;
            this.ForecastError += score.ForecastError;
            this.RealError += score.RealError;
            this.LackReal += score.LackReal;
            
        }
    }
}
